$(function(){
	
	//获取到查询类型
	var type = $("#type").val();
	//获取到class位findByStatus的li对象
	var arr = $(".findByStatus");
	//一次查看哪一个的data-status和查询类型相同
	for (var i = 0; i < arr.length; i++) {
		var str = $(arr[i]).attr("data-status") + "";
		//如果相等，则加上下划线
		if(str == type){
			$(arr[i]).parent().addClass("active");
			break;
		}
	}
	var $container = $(".status-list-container")
	active =  $container.find(".active"),
	activeX = active.offset().left
	containerX = $container.scrollLeft(),
	liWidth=active.width();
	$container.scrollLeft(activeX-liWidth*2);
})
$(".findByStatus").click(function(){
	
	var type = $(this).attr("data-status");
	var url = base_url + "customer/toNormalOrdersPage";
	var params = {
		"type" : type
	}
	post(url, params);

})


//点击退款按钮，弹出框
$(".tuikuanTankuang").on("click",function(e){
	
	// 获取到参数
	var ordersItemId = $(this).parents(".product").attr("id");
	//设置参数到隐藏的文本框中
	$("#data-ordersItemId").attr("value", ordersItemId);
	$("#data-type").attr("value", "0");//退款
	//客户端弹框初始化（必须）
	$("#applyOrRefuse").before("<div class='shadow'></div>");
	$("#applyOrRefuse").fadeIn();
	//点击遮罩关闭弹框
	clickShadow(function() {
		$("#applyOrRefuse").fadeOut();
	})
});

//申请退款
function applyRefund() {
	
	// 获取到参数
	var ordersItemId = $("#data-ordersItemId").val();
	var reason = $("#reason").val();
	//申请理由不能为空
	if(reason == null || reason =="" || reason ==undefined){
		alert("申请理由不能给为空");
	}else{
		//url
		var url = base_url + "customer/applyRefund";
	    //惨呼
		var params = {
			"ordersItemId" : ordersItemId,
			"reason" : reason
		}
		//进行请求
		post(url, params);
	}
}
//取消退款
$(".cancelRefund").on("click",function(e){
	// 获取到订单项id
	var ordersItemId = $(this).parents(".product").attr("id");
	//封装参数
	var params = {
			"ordersItemId":ordersItemId
	}
	//请求路径
	var url = base_url + "customer/cancelRefund";
	post(url, params);
})

//点击退货按钮
$(".tuiHuoTankuang").on("click",function(e){
	
	// 获取到参数
	var ordersItemId = $(this).parents(".product").attr("id");
	//设置参数到隐藏的文本框中
	$("#data-ordersItemId").attr("value", ordersItemId);
	$("#data-type").attr("value", "1");//退款
	//客户端弹框初始化（必须）
	$("#applyOrRefuse").before("<div class='shadow'></div>");
	$("#applyOrRefuse").fadeIn();
	//点击遮罩关闭弹框
	clickShadow(function() {
		$("#applyOrRefuse").fadeOut();
	})
});

//申请退货
function applyReturnGoods() {
	
	// 获取到参数
	var ordersItemId = $("#data-ordersItemId").val();
	var reason = $("#reason").val();
	//申请理由不能为空
	if(reason == null || reason =="" || reason ==undefined){
		alert("申请理由不能给为空");
	}else{
		//url
		var url = base_url + "customer/applyReturnGoods";
	    //惨呼
		var params = {
			"ordersItemId" : ordersItemId,
			"reason" : reason
		}
		//进行请求
		post(url, params);
	}
}

//取消退款
$(".cancelReturnGoods").on("click",function(e){
	// 获取到订单项id
	var ordersItemId = $(this).parents(".product").attr("id");
	//封装参数
	var params = {
			"ordersItemId":ordersItemId
	}
	//请求路径
	var url = base_url + "customer/cancelReturnGoods";
	post(url, params);
})

// 点击确定按钮
$("#applyOrRefuse").on("click", ".btn", function() {

	var reason = $("#reason").val();
	if (reason == "" || reason == null || reason == undefined) {		
		if(!$(this).hasClass("cancel")){
			alert("理由不能为空");
		}
	} else {
		var type = $("#data-type").val();
		switch (type) {
		case '0':
			applyRefund();// 申请退款
			break;
		case '1':
			applyReturnGoods();// 申请退货
			break;
		default:
			break;
		}
		//点击按钮关闭弹框
		$(".shadow").remove();
		$("#applyOrRefuse").fadeOut();
	}

})

// 点击确定按钮
$("#applyOrRefuse").on("click", ".cancel", function() {

		//点击按钮关闭弹框
		$(".shadow").remove();
		$("#applyOrRefuse").fadeOut();

})

/**
 * 进行签收
 * 
 * @param obj
 */
function sign(obj) {

	// 订单id
	var ordersId = $(obj).parents(".order").attr("id");
	var params = {
		"ordersId" : ordersId
	}
	var url = base_url + "customer/signOrder";
	post(url, params);

}

$(".returnGoodsing").on("tap",function(){
	$("#returnGoodsing-msg").before("<div class='shadow'></div>").fadeIn();
	clickShadow(function() {
		$("#returnGoodsing-msg").fadeOut();
	})
    var name = $(this).parents(".order").attr("data-name");
	var phone= $(this).parents(".order").attr("data-phone");
	var address= $(this).parents(".order").attr("data-address");
	var str = name + phone + address ; 
	if(str==null || str=="" || str==undefined){
		$("#refundGoodTip").html("<p>请联系卖家获取退货信息</p></br></br></br>");
	}else{
		$("#contactName").text("卖家姓名：" +name);
		$("#contactWay").text("卖家电话：" +phone);
		$("#contactAddress").text("买家收货地址："+address );	
	}
	$("#returnGoodOrdersId").attr("value",$(this).parents(".order").attr("id"));
	$("#returnGoodOrdersItemsId").attr("value",$(this).parents(".product").attr("id"));
	
})

/**
 * 进行返货
 */
$(".returnGoodCommit").click(function(e){
	
	// 订单id
	var ordersItemId = $("#returnGoodOrdersItemsId").attr("value");
	//获取到物流公司的消息
	var selectObj = $("#expressCompany")[0];
	var expressCompanyId = selectObj.options[selectObj.selectedIndex].value;
	var transportNumber = $("#transportNumber").val();
	//如果物流单号为空，则不能提交
	if(transportNumber==null || transportNumber=="" ||transportNumber==undefined){ 
		tip.content("物流单号不能为空").show();
	}else{		
			var params = {
				"ordersItemId" : ordersItemId,
				"expressCompanyId" : expressCompanyId,
				"transportNumber" : transportNumber
			}
			var url = base_url + "customer/fanhuo";
			post(url, params);
	}

})

$(".normalOrderPay").on("click",function(){
	
	// 订单id
	var ordersId = $(this).parents(".order").attr("id");
	var params = {
		"ordersId" : ordersId
	};
	var url = base_url + "pay/payNormalProductOrdersInOrdersList";
	post(url, params);
})

$(".groupOrderPay").on("click",function(){
	
	// 订单id
	var ordersId = $(this).parents(".order").attr("id");
	var packageTourRankId = $(this).attr("data-packageTourRankId");
	var params = {
		"ordersId" : ordersId
	};
	var url = base_url + "pay/payOpenPackageTourRankOrdersInOrdersList";
	if(packageTourRankId != null && packageTourRankId !="" && packageTourRankId!=undefined){
		url = base_url + "pay/payJoinPackageTourRankOrdersInOrdersList";
	}
	post(url, params);
})

$(".discountOrderPay").on("click",function(){
	
	// 订单id
	var ordersId = $(this).parents(".order").attr("id");
	var params = {
		"ordersId" : ordersId
	};
	var url = base_url + "pay/payDiscountProductOrdersInOrdersList";
	post(url, params);
})

/*$(".crowdFundingOrderPay").on("click",function(){
	
	// 订单id
	var ordersId = $(this).parents(".order").attr("id");
	var params = {
		"ordersId" : ordersId
	};
	var url = base_url + "pay/payOrdersInOrdersList";
	post(url, params);
})*/

$(".spikeRushOrderPay").on("click",function(){
	
	// 订单id
	var ordersId = $(this).parents(".order").attr("id");
	var params = {
		"ordersId" : ordersId
	};
	var url = base_url + "pay/payRushProductOrdersInOrdersList";
	post(url, params);
})


/**
 * 关闭订单
 * 
 * @param obj
 */
function closeOrder(obj) {

	// 订单id
	var ordersId = $(obj).parents(".order").attr("id");
	
	var params = {
		"ordersId" : ordersId
	};
	var url = base_url + "customer/closeOrder";
	post(url, params);

}

/**
 * 退款详情
 */
$(".tuikuan").click(function(){
	
	//客户端弹框初始化（必须）
	$("#detail").before("<div class='shadow'></div>");
	$("#detail").fadeIn();
	//点击遮罩关闭弹框
	clickShadow(function() {
		$("#detail").fadeOut();
	})
	// 订单id
	var ordersItemId = $(this).parents(".product").attr("id");
	$("#detailContent").html("</p>加载中...</p>");
	//请求成功
	var success = function(data){
	
		$("#detailHead").text("退款详情");
		$("#detailContent").html("");
		if(data.applyReason!=null && data.applyReason!=""){
			
			$("#detailContent").append("</p>申请理由：" + data.applyReason+"</p>");
			$("#detailContent").append("</br>");
		}
        if(data.refuseReason!=null && data.refuseReason!=""){
        	$("#detailContent").append("</p>拒绝理由：" + data.refuseReason+"</p>");
        	$("#detailContent").append("</br>");
		}
	}	
	//请求失败
	var error = function(){
		$("#detailContent").html("</p>请求失败</p>");
	}	
	var url = base_url + "customer/refundDetail";
	var params = "ordersItemId=" + ordersItemId;
	doAjax("POST", url, params, success, error, true, false);
	
})

/**
 * 退款详情
 */
$(".tuihuo").click(function(){

	// 订单id
	var ordersId = $(this).parents(".order").attr("id");	
	var ordersItemId = $(this).parents(".product").attr("id");
	//客户端弹框初始化（必须）
	$("#detail").before("<div class='shadow'></div>");
	$("#detail").fadeIn();
	$("#detailContent").html("加载中...");
	//点击遮罩关闭弹框
	clickShadow(function() {
		$("#detail").fadeOut();
	})
	//请求成功
	var success = function(data){
		$("#detailContent").html("");
		$("#detailHead").text("退货退款详情");		
        if(data.address!=null && data.address!=""){
			
			$("#detailContent").append("</p>退货详情：" + data.address+"</p>");
			$("#detailContent").append("</br>");
		}
		if(data.applyReason!=null && data.applyReason!=""){
			
			$("#detailContent").append("</p>申请理由：" + data.applyReason+"</p>");
			$("#detailContent").append("</br>");
		}
        if(data.refuseReason!=null && data.refuseReason!=""){
        	$("#detailContent").append("</p>拒绝理由：" + data.refuseReason+"</p>");
        	$("#detailContent").append("</br>");
		}
	}	
	//请求失败
	var error = function(){
		$("#detailContent").html("</p>请求失败</p>");
	}	
	var url = base_url + "customer/returnGoodsDetail";
	var params = "ordersItemId="+ordersItemId;
	doAjax("POST", url, params, success, error, true, false);
	
})

// 点击确定按钮
$("#detail").on("click", ".btn", function() {

	//点击按钮关闭弹框
	$(".shadow").remove();
	$(".confirm").fadeOut();

})

//开团详情
$(".groupDetail").on("click",function(){
	
	//获取到开团的id
	var packageTourRankId = $(this).attr("data-id");
	//获取到订单的id
	var ordersId = $(this).parents(".order").attr("id");
	//封装参数
	var params = {
			"packageTourRankId":packageTourRankId,
			"ordersId":ordersId
	}
	var url = base_url + "customer/toPackageTourDetailPage";
	post(url, params);
})
//开团详情
$(".crowFundingDetail").on("click",function(){
	
	//获取到订单的id
	var ordersId = $(this).parents(".order").attr("id");
	var params = {
			"ordersId":ordersId
	}
	var url = base_url + "customer/tocrowdFundingDetailPage";
	post(url, params);
})

////邀人参团
//$(".invite").on("click",function(){
//	
//	// 订单id
//	var ordersId = $(obj).parents(".order").attr("id");
//	var params = {
//		"ordersId" : ordersId
//	}
//	var url = base_url + "customer/invite";
//	post(url, params);
//})

//点击邀人参团按钮
	    $("#groupInvite").on("click",function(){
	    	var method = "GET";
			var url = base_url+"cors/getJsapiPackage";
			var params = {
				"url" : location.href.split('#')[0]
			};
			var isAsync = true;
			var isFile = false;
			doAjax(method,url,params,successCallback,errorCallback,isAsync,isFile);
	    });
	    
		function successCallback(data){
			shareMessage(data);
			tip.content("点击右上角进行分享").show();
		};
		
		function errorCallback(){
			alert("Error!");
		};	    
		function shareMessage(jsapiPackage){
			 var id = $("#packageTourRankId").val();
			 var productSurfacePlot = $("#productSurfacePlot").val();
			 var productName = $("#productName").val();
			 var desc = "加入我的军团吧";
			 var params = "packageTourRankId="+id;
			 var redirect_url = "http://test4.productshow.cn/alphaking/customer/toPackageTourDetailPage?"+params; 
			 wx.config({
				debug: jsapiPackage.debug, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
				appId: jsapiPackage.appId, // 必填，公众号的唯一标识
				timestamp: jsapiPackage.timestamp, // 必填，生成签名的时间戳
				nonceStr: jsapiPackage.nonceStr, // 必填，生成签名的随机串
				signature: jsapiPackage.signature,// 必填，签名，见附录1
				jsApiList: [  // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
				   'onMenuShareTimeline',
				   'onMenuShareAppMessage'
				] 
			});
			
			wx.ready(function(){
				wx.onMenuShareAppMessage({
				    title: ''+productName,
				    desc: ''+desc,
				    link: ''+redirect_url, // 分享链接
				    imgUrl: '' +productSurfacePlot, // 分享图标
		            success: function (res) {
		            }
				});
				
				wx.onMenuShareTimeline({
		            title: ''+productName,
		            link: ''+redirect_url,
		            imgUrl: ''+productSurfacePlot,
		            success: function (res) {
			       }
			    });
			});
			
			wx.error(function(res){
				alert("Error!");
			});
		}	    
